package chen.web.controller.forum;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import chen.web.controller.ResourceNotFoundException;
import chen.web.forum.Category;
import chen.web.forum.CategoryManager;


public class CategoryController {
	private @Autowired CategoryManager cm = null;
	
	private Category getCategory(int id){
		Category category = cm.get(id);
		if(category == null){
			throw new ResourceNotFoundException();
		}
		return category;
	}
	
	@RequestMapping(value="/category/{id}/children", method=RequestMethod.GET)
	public @ResponseBody JSONResponse get(@PathVariable int id){
		Category category = getCategory(id);
		return JSONResponse.successed(category.getChildren());
	}
}
